<script setup>
import {reactive, onMounted} from "vue"
import registerElements from "./registerElements.js"
import {useRoute, useRouter} from "vue-router"
import * as api from "./api.js"

registerElements(window.$app)
const route = useRoute()
const router = useRouter()

const state = reactive({
    elements: [],
})

const back = () => {
    router.back()
}

const print = () => {
    window.print()
}

/**
 * 加载数据
 */
const loadInfo = () => {
    state.isLoading = true
    api.GetProgramReport({id: route.query.id}).then(({data}) => {
        state.isLoading = false
        state.info = data
        state.elements = data.content ? JSON.parse(data.content) : []
    })
}

onMounted(() => {
    loadInfo()
})
</script>

<template>
    <div class="tool-bar">
        <div class="button" @click="back">
            <b-icon name="bi-chevron-left" size="18" style="margin-right: 2px" />
            返回
        </div>
        <div class="button button-primary" @click="print">
            <b-icon name="bi-printer" size="16" style="margin-right: 6px" />
            打印
        </div>
    </div>
    <div v-loading="state.isLoading" class="Preview">
        <div class="page">
            <component :is="elem.type" v-for="elem in state.elements" :key="elem.id" :elem="elem" />
        </div>
    </div>
</template>

<style lang="scss" scoped>
.Preview {
    width: 100vw;
    min-height: 100vh;
    background: #fff;
    box-sizing: border-box;
    //padding: 20px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;

    .page {
        background: #fff;
        width: 210mm;
        box-sizing: border-box;
        padding: 20px;
    }
}

.tool-bar {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    position: fixed;
    left: 0;
    top: 0;
    padding: 10px;

    .button {
        padding: 4px 10px;
        background: #fff;
        margin-right: 10px;
        border-radius: 6px;
        user-select: none;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        border: 1px solid var(--el-color-primary);

        &:active {
            opacity: 0.8;
        }
    }
    .button-primary {
        background: var(--el-color-primary);
        color: #fff;
    }
}

@page {
    margin: 0;
}

@media print {
    .tool-bar {
        display: none;
    }
}
</style>